package cn.tedu.controller;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.tedu.entity.Province;
/**
 * @author 作者:bjzhangjian
 * @email 邮箱：bjzhangjian@tedu.cn
 * @version 创建时间：2021年1月26日 上午11:25:41
 * @description 描述：爬取疫情数据
 */
public class TestEpidemicData {

	public static void main(String[] args) {
		try {
			new TestEpidemicData().test02();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void test02() throws Exception {
		//System.out.println("单元测试方法");
		//1、爬取的网页的地址
		String url = "https://ncov.dxy.cn/ncovh5/view/pneumonia";
		/**
		 * Jsoup：
		 * 		java中爬虫：直接爬取   ajax二次请求 
		 * 		connect: 建立连接：代码和浏览器/服务器
		 * 			connect(url)
		 * 		userAgent()：模拟浏览器去发送请求地址
		 * 		get()：返回的是html的一个文本网页了  document
		 * 禁止爬虫：
		 */
		Document doc = Jsoup.connect(url)
				.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36")
				.get();
		//System.out.println(doc);
		String resultStr = doc.html();
		//System.out.println(resultStr);
		//了解----
		String regex = "window.getAreaStat = (.*?)\\}(?=catch)";
		Pattern patt = Pattern.compile(regex);
		Matcher matcher = patt.matcher(resultStr);
		String jsonStr = "";
		while (matcher.find()) {
			jsonStr = matcher.group(1);
			//System.out.println(jsonStr);
			//json数据的处理 ObjectMapper
			ObjectMapper mapper = new ObjectMapper();
			//JsonNode jn;集合
			JsonNode jn = mapper.readTree(jsonStr);//
			
			for (int i = 0; i < jn.size(); i++) {
				//创建一个实体类对象pojo   
				Province prov = new Province();
				//System.out.println("i  : " + i);//34个对象
				String provinceName = jn.get(i).get("provinceName").asText();
				int currentConfirmedCount = jn.get(i).get("currentConfirmedCount").asInt();
				int confirmedCount = jn.get(i).get("confirmedCount").asInt();
				int curedCount = jn.get(i).get("curedCount").asInt();
				int deadCount = jn.get(i).get("deadCount").asInt();
				//将爬取出来的数据放到实体类对象中 prov  object对象
				prov.setProvinceName(provinceName);
				prov.setCurrentConfirmedCount(currentConfirmedCount);
				prov.setConfirmedCount(confirmedCount);
				prov.setCuredCount(curedCount);
				prov.setDeadCount(deadCount);
				System.out.println(prov);
				//				System.out.println(provinceName + 
//						",现存确诊：" + currentConfirmedCount + 
//						",累计确诊：" + confirmedCount +
//						",治愈人数：" + curedCount +
//						",死亡人数：" + deadCount
//						);
				
				
			}
		}
	}

	//单元测试方法
	@Test
	public void test01() throws Exception {
		//System.out.println("单元测试方法");
		//1、爬取的网页的地址
		String url = "https://ncov.dxy.cn/ncovh5/view/pneumonia";
		/**
		 * Jsoup：
		 * 		java中爬虫：直接爬取   ajax二次请求 
		 * 		connect: 建立连接：代码和浏览器/服务器
		 * 			connect(url)
		 * 		userAgent()：模拟浏览器去发送请求地址
		 * 		get()：返回的是html的一个文本网页了  document
		 * 禁止爬虫：
		 */
		Document doc = Jsoup.connect(url)
				.userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36")
				.get();
		//System.out.println(doc);
		String resultStr = doc.html();
		//System.out.println(resultStr);
		//了解----
		String regex = "window.getAreaStat = (.*?)\\}(?=catch)";
		Pattern patt = Pattern.compile(regex);
		Matcher matcher = patt.matcher(resultStr);
		String jsonStr = "";
		while (matcher.find()) {
			jsonStr = matcher.group(1);
			System.out.println(jsonStr);
			//json数据的处理 ObjectMapper
			ObjectMapper mapper = new ObjectMapper();
			//JsonNode jn;集合
			JsonNode jn = mapper.readTree(jsonStr);//
			for (int i = 0; i < jn.size(); i++) {
				//System.out.println("i  : " + i);//34个对象
				String provinceName = jn.get(i).get("provinceName").asText();
				System.out.println(provinceName);
			}

		}

	}



}
